﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
namespace ERP.Classes
{
    public class cs_erp_khuvucInfo
    {
        private int _id_khuvuc;
        private string _makhuvuc;
        private string _tenkhuvuc;
        private int _soluongban;
        private int _makho;

        public cs_erp_khuvucInfo()
        {
            this._id_khuvuc = 0;
            this._makhuvuc = " ";
            this._tenkhuvuc = " ";
            this._soluongban = 0;
            this._makho = 0;

        }
        public int id_khuvuc
        {
            get
            {
                return this._id_khuvuc;
            }
            set
            {
                this._id_khuvuc = value;
            }
        }
        public string makhuvuc
        {
            get
            {
                return this._makhuvuc;
            }
            set
            {
                this._makhuvuc = value;
            }
        }
        public string tenkhuvuc
        {
            get
            {
                return this._tenkhuvuc;
            }
            set
            {
                this._tenkhuvuc = value;
            }
        }
        public int soluongban
        {
            get
            {
                return this._soluongban;
            }
            set
            {
                this._soluongban = value;
            }
        }
        public int makho
        {
            get
            {
                return this._makho;
            }
            set
            {
                this._makho = value;
            }
        }

        public int ExcuteStoreProcedure(string storeProcedure, int option)
        {
            string[] paraName = new string[6];
            paraName[0] = "@id_khuvuc";
            paraName[1] = "@makhuvuc";
            paraName[2] = "@tenkhuvuc";
            paraName[3] = "@soluongban";
            paraName[4] = "@makho";
            paraName[5] = "@option";
            object[] paraValue = new object[6];
            paraValue[0] = this._id_khuvuc;
            paraValue[1] = this._makhuvuc;
            paraValue[2] = this._tenkhuvuc;
            paraValue[3] = this._soluongban;
            paraValue[4] = this._makho;
            paraValue[5] = option;
            csCSDL csdl = new csCSDL();
            int i = 0;
            i = csdl.CreateParameters(paraName, 6, paraValue);
            return csdl.ExcuteStoreProcedure(storeProcedure);
        }
        public int ExcuteProcedure(int option)
        {
            return this.ExcuteStoreProcedure("Proc_ERP_KHUVUC", option);
        }

        public int Get_Info()
        {
            csCSDL csdl = new csCSDL();
            DataSet ds = new DataSet("ds");
            try
            {
                ds = csdl.OpenTable("ERP_KHUVUC", "");
                if (ds == null || ds.Tables[0].Rows.Count == 0)
                {
                    return 0;
                }
                this._id_khuvuc = Convert.ToInt32(ds.Tables[0].Rows[0]["id_khuvuc"].ToString().Trim());
                this._makhuvuc = ds.Tables[0].Rows[0]["makhuvuc"].ToString().Trim();
                this._tenkhuvuc = ds.Tables[0].Rows[0]["tenkhuvuc"].ToString().Trim();
                this._soluongban = Convert.ToInt32(ds.Tables[0].Rows[0]["soluongban"].ToString().Trim());
                this._makho = Convert.ToInt32(ds.Tables[0].Rows[0]["makho"].ToString().Trim());
                return 1;
            }
            catch
            {
                return -1;
            }
        }
        public DataSet Get_List()
        {
            csCSDL csdl = new csCSDL();
            DataSet ds = new DataSet("ds");
            try
            {
                string sql = "select id_khuvuc, makhuvuc, tenkhuvuc, soluongban, makho"
                + " from ERP_KHUVUC";
                ds = csdl.SelecSQL(sql);

                return ds;
            }
            catch
            {
                return null;
            }
        }

        public DataSet layDoanhthuBanhang(DateTime tungay, DateTime denngay, int khuvuc, int opt)
        {
            csCSDL csdl = new csCSDL();
            DataSet ds = new DataSet("ds");
            int tn = tungay.Year * 12 * 30 + tungay.Month * 30 + tungay.Day;
            int dn = denngay.Year * 12 * 30 + denngay.Month * 30 + denngay.Day;
            try
            {
                string sql = "";
                if (opt == 1)
                {
                    sql = "select row_number() over (order by b.ngayban) as stt, a.tenkhachhang, b.ngayban, b.conlai, b.datra, b.conno"
                    + " from ERP_KHACHHANG a, ERP_HOADON b"
                    + " where b.trangthai = 5 and a.id_kh = b.id_kh "
                    + " and year(b.ngayban)*12*30 + month(b.ngayban)*30 + day(b.ngayban) >= " + tn
                    + " and year(b.ngayban)*12*30 + month(b.ngayban)*30 + day(b.ngayban) <= " + dn;
                }
                else
                {
                    sql = "select row_number() over (order by b.ngayban) as stt, a.tenkhachhang, b.ngayban, b.conlai, b.datra, b.conno"
                    + " from ERP_KHACHHANG a, ERP_HOADON b"
                    + " where a.id_kh = b.id_kh "
                    + " and year(b.ngayban)*12*30 + month(b.ngayban)*30 + day(b.ngayban) >= " + tn
                    + " and year(b.ngayban)*12*30 + month(b.ngayban)*30 + day(b.ngayban) <= " + dn;
                }
                ds = csdl.SelecSQL(sql);

                return ds;
            }
            catch
            {
                return null;
            }
        }
    }
}